package command

import (
	"ssadmin/common/constant"
	"ssadmin/internal/admin/models"
	"strings"
	"time"

	"gitee.com/sansaniot/ssiot-core/httpmvc/jwtauth/user"
	"github.com/gin-gonic/gin"
	uuid "github.com/satori/go.uuid"
)

type RoleInsertReq struct {
	RoleId     string    `json:"roleId" comment:"角色id"`
	RoleName   string    `json:"roleName" comment:"角色名称"`
	Remark     string    `json:"remark" comment:"角色描述"`
	CreateTime time.Time `json:"createTime" comment:"创建时间"`
	ModifyTime time.Time `json:"modifyTime" comment:"修改时间"`
	CreatorId  string    `json:"creatorId" comment:""`
	Creator    string    `json:"creator" comment:""`
	Type       string    `json:"type" comment:""`
	MenuId     string    `json:"menuIds" comment:""`
	SystemId   string    `json:"-" comment:""`
}

func (s *RoleInsertReq) Bind(c *gin.Context) {
	//获取扩展属性（自定义扩展）
	s.Creator = user.Get(c, constant.CacheKeyUserName).(string)
	s.CreatorId = user.Get(c, constant.CacheKeyUserId).(string)
}

func (s *RoleInsertReq) Generate(model *models.Role) {
	if len(s.RoleId) == 0 {
		model.RoleId = uuid.NewV4().String()
		s.RoleId = model.RoleId
	} else {
		model.RoleId = s.RoleId
	}
	model.CreateTimeDts = time.Now()
	model.ModifyTimeDts = time.Now()
	model.RoleName = s.RoleName
	model.Remark = s.Remark
	model.CreatorId = s.CreatorId
	model.Creator = s.Creator
	model.Type = s.Type
}

func (s *RoleInsertReq) GetId() interface{} {
	return s.RoleId
}

type RoleUpdateReq struct {
	RoleId   string `json:"id" comment:"角色id"`
	RoleName string `json:"roleName" comment:"角色名称"`
	Remark   string `json:"remark" comment:"角色描述"`
	Type     string `json:"type" comment:""`
	MenuIds  string `json:"menuIds" comment:""`
	SystemId string `json:"-" comment:""`
}

func (s *RoleUpdateReq) Generate(model *models.Role) {
	model.RoleId = s.RoleId
	model.RoleName = s.RoleName
	model.Remark = s.Remark
	model.ModifyTimeDts = time.Now()
	model.Type = s.Type
}

func (s *RoleUpdateReq) GetId() interface{} {
	return s.RoleId
}

type RoleMenu struct {
	RoleId string
	MenuId string
}

// RoleDeleteReq 功能删除请求参数
type RoleDeleteReq struct {
	RoleIds string `json:"ids"`
}

func (s *RoleDeleteReq) GetId() interface{} {
	ids := strings.Split(s.RoleIds, ",")
	return ids
}
